package com.zzc.qqchat.controller;


import com.zzc.qqchat.repository.*;
import com.zzc.qqchat.domain.*;
import com.zzc.qqchat.domain.FriendsList;
import com.zzc.qqchat.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;


@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @Resource
    SingleToSingleRep singleToSingleRep;

    @Resource
    GroupchatRep groupchatRep;

    @Resource
    FriendsRep friendsRep;

    @Resource
    UserInforRep userInforRep;

    @Resource
    GroupRep groupRep;

    @GetMapping("/test")
    public String mes(Long id, ModelMap model){
        model.addAttribute("id",id);
        return "/message/test01";
    }

    @ResponseBody
    @GetMapping("/history1")
    public String history(int id) throws InterruptedException {
        ArrayList<Message> message = new ArrayList<>();
        ArrayList<SingleToSingle> theHistory = singleToSingleRep.findByReceId(id,id);
        ArrayList<Group> groups = groupRep.findByUserId(id);
        for(int i=0;i<theHistory.size();i++){
            message.add(new Message(theHistory.get(i).getText(),theHistory.get(i).getSenderId(),theHistory.get(i).getReceiverId(),theHistory.get(i).getDate(),theHistory.get(i).getSenderName(),"sendmessage"));
            // message.add(new Message("1","2","1"));
            simpMessagingTemplate.convertAndSendToUser(String.valueOf(id),"/message/get",message.get(i));
            //System.out.println(message.get(i));
        }
        message.clear();
        theHistory.clear();
        for(int i=0;i<groups.size();i++){
            ArrayList<GroupChat> groupChats = groupchatRep.findBygroupId(groups.get(i).getGroupId());
            for(int j=0;j<groupChats.size();j++){
                int x =0;
                message.add(new Message(groupChats.get(j).getContent(),groupChats.get(j).getSendId(),groupChats.get(j).getTo(),groupChats.get(j).getTime(),"","sendgroupmessage"));
                simpMessagingTemplate.convertAndSendToUser(String.valueOf(id),"/message/get",message.get(j));
                Thread.sleep(200);
            }
            message.clear();
        }
        groups.clear();
        return "ok";
    }

    @ResponseBody
    @PostMapping("/friends")
    public ArrayList<FriendsList> friends(int userId){
        ArrayList<FriendsList> friendsLists = new ArrayList<>();
        ArrayList<Friends> friends = friendsRep.findByuserId(userId);
        ArrayList<UserInfor> userInfors = new ArrayList<>();
        for(int i=0;i<friends.size();i++){
           userInfors.add(userInforRep.findByuserId(friends.get(i).getFriendId()));
        }
        for(int i=0;i<userInfors.size();i++){
            friendsLists.add(new FriendsList(userInfors.get(i).getUserId(),userInfors.get(i).getUserName(),userInfors.get(i).getPicture()));
        }

        return friendsLists;
    }

    @MessageMapping("/message")
    public void message(Message message){
        if (message.getMessageType().equals("sendgroupmessage")){

            ArrayList<Group> users = groupRep.findByGroupId(message.getToUserId());
            for(int i=0;i<users.size();i++){
                if(users.get(i).getUserId()!=message.getFromUserId()){
                    simpMessagingTemplate.convertAndSendToUser(String.valueOf(users.get(i).getUserId()),"/message/get",message);
                }
            }

            GroupChat gc = new GroupChat();
            gc.setSendId(message.getFromUserId());
            gc.setContent(message.getContent());
            gc.setTime(message.getTime());
            gc.setTo(message.getToUserId());
            groupchatRep.save(gc);
        }else {
            simpMessagingTemplate.convertAndSendToUser(String.valueOf(message.getToUserId()),"/message/get",message);
            SingleToSingle sts =new SingleToSingle();
            sts.setReceiverId(message.getToUserId());
            sts.setSenderId(message.getFromUserId());
            sts.setText(message.getContent());
            sts.setDate(message.getTime());
            sts.setSenderName(message.getUserName());
            singleToSingleRep.save(sts);
        }

    }

}
